package com.pioneer.swing.ui;

import com.pioneer.beans.Key;
import com.pioneer.beans.Keyed;
import com.pioneer.beans.ContainerKey;

import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.Box;
import javax.swing.BoxLayout;


/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class ManifestEntryFrame extends JInternalFrame {
	public JTable containerListTable;
	public JButton editButton;
	public JButton deleteButton;
	public JButton newButton;

	public JButton createManifestButton;
	

	private JLabel manifestTypeLabel = new JLabel("");
	public JTextField jobTextField=new JTextField(10);
	public JTextField vesselTextField=new JTextField(15);
	public JTextField etdTextField=new JTextField(10);
	public JTextField etaTextField=new JTextField(10);
	public JTextField notesTextField=new JTextField(50);
	public JTextField subTotalTextField=new JTextField(7);
	public JTextField freightTextField=new JTextField(7);
	public JTextField truckingTextField=new JTextField(7);
	public JTextField miscTextField=new JTextField(7);
	public JTextField grandTotalTextField=new JTextField(7);
    public ManifestEntryFrame() {
    	super(UIConstants.TITLE_MANIFEST_ENTRY,true,true);
		this.setName(UIConstants.TITLE_MANIFEST_ENTRY);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

    	JPanel mainPanel=new JPanel(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createEmptyBorder(
                                        15, //top
                                        15, //left
                                        5, //bottom
                                        15) //right
										);
		JPanel centerPanel=new JPanel();
		centerPanel.setLayout(new BorderLayout());

		JPanel headerPanel=new JPanel(new GridLayout(3,1));
		JPanel firstRow=new JPanel();
		firstRow.add(new JLabel("Manifest Type:"));		
		manifestTypeLabel.setFont(new Font(null,Font.BOLD,14));
		firstRow.add(manifestTypeLabel);
		firstRow.add(new JLabel("Job#: "));
		firstRow.add(jobTextField);
		firstRow.add(new JLabel("ETD: "));
		firstRow.add(etdTextField);
		firstRow.add(new JLabel("ETA: "));
		firstRow.add(etaTextField);
		headerPanel.add(firstRow);
		JPanel secondRow=new JPanel();
		secondRow.add(new JLabel("Container Notes: "));
		secondRow.add(notesTextField);
		headerPanel.add(secondRow);
		JPanel thirdRow=new JPanel();
		thirdRow.add(new JLabel("Vessel: "));
		thirdRow.add(vesselTextField);
		createManifestButton=new JButton(UIConstants.COMMAND_SAVE_MANIFEST_BUTTON);
		thirdRow.add(createManifestButton);
		headerPanel.add(thirdRow);
		centerPanel.add(headerPanel,BorderLayout.NORTH);

		// Table of containers here
		
		containerListTable=new JTable();
		containerListTable.setFocusable(false);
		JScrollPane scrollPane=new JScrollPane(containerListTable);
		centerPanel.add(scrollPane,BorderLayout.CENTER);
		
		// add the footer area. consists of a button panel on the left and a totals panel on the right

		JPanel footerPanel=new JPanel();
		JPanel buttonPanel=new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
		editButton=new EditDeleteButton(UIConstants.COMMAND_EDIT_CONTAINER);
		buttonPanel.add(editButton);
		buttonPanel.add(Box.createHorizontalStrut(5));
		deleteButton=new EditDeleteButton(UIConstants.COMMAND_DELETE_CONTAINER);
		buttonPanel.add(deleteButton);
		buttonPanel.add(Box.createHorizontalStrut(5));
		newButton=new NewButton(UIConstants.COMMAND_NEW_CONTAINER);
		buttonPanel.add(newButton);
		footerPanel.add(buttonPanel);

		JPanel totalsPanel=new JPanel(new GridLayout(5,2));
		totalsPanel.add(new JLabel("Subtotal:"));
		subTotalTextField.setHorizontalAlignment(JTextField.RIGHT);
		subTotalTextField.setEditable(false);
		subTotalTextField.setFocusable(false);
		totalsPanel.add(subTotalTextField);
		totalsPanel.add(new JLabel("Freight Charge:"));
		freightTextField.setHorizontalAlignment(JTextField.RIGHT);
		freightTextField.setText("0");
		totalsPanel.add(freightTextField);
		totalsPanel.add(new JLabel("Trucking Charge:"));
		truckingTextField.setHorizontalAlignment(JTextField.RIGHT);
		truckingTextField.setText("0");
		totalsPanel.add(truckingTextField);
		totalsPanel.add(new JLabel("Misc Charge:"));
		miscTextField.setHorizontalAlignment(JTextField.RIGHT);
		miscTextField.setText("0");
		totalsPanel.add(miscTextField);		
		totalsPanel.add(new JLabel("Total:"));
		grandTotalTextField.setHorizontalAlignment(JTextField.RIGHT);
		grandTotalTextField.setEditable(false);
		grandTotalTextField.setFocusable(false);
		totalsPanel.add(grandTotalTextField);
		footerPanel.add(totalsPanel);
		centerPanel.add(footerPanel,BorderLayout.SOUTH);	
		mainPanel.add(centerPanel);
	   	this.getContentPane().add(mainPanel,BorderLayout.CENTER);
    }
	
	public void setManifestType(String manifestType){		
		this.manifestTypeLabel.setText(manifestType);
	}
	
	public String getManifestType(){
		return this.manifestTypeLabel.getText();		
	}


	// have this button implement table model change listener so when the cell value changes the selected jobnum changes

	public class NewButton
	extends JButton
	implements Keyed
	{
		protected String selectedJobNum;

		NewButton(String label)
		{
			super(label);
		}

		protected void fireActionPerformed(ActionEvent event)
		{
			selectedJobNum=getSelectedJobNum();
			//System.out.println("Found selectedJobNum "+selectedJobNum);
			super.fireActionPerformed(event);
		}

		public Key getKey()
		{
			//System.out.println("Returning key "+selectedJobNum);
			return new ContainerKey(selectedJobNum,getManifestType(),null);
		}

	}

	public class EditDeleteButton
	extends JButton
	implements Keyed
	{
		protected String selectedJobNum;
		protected String containerId;

		EditDeleteButton(String label)
		{
			super(label);
		}

		protected void fireActionPerformed(ActionEvent event)
		{
			selectedJobNum=getSelectedJobNum();
			containerId=getSelectedId();
			//System.out.println("Found containerId "+containerId);
			//System.out.println("Firing event for two part key");
			super.fireActionPerformed(event);
		}

		public Key getKey()
		{
			//System.out.println("Returning keys "+selectedJobNum+", "+containerId);
			return new ContainerKey(selectedJobNum,getManifestType(),containerId);
		}
	}

	private String getSelectedJobNum()
	{
		return jobTextField.getText();
	}

	private String getSelectedId()
	{
		int row=containerListTable.getSelectedRow();
		if (row>-1) {
			return containerListTable.getModel().getValueAt(row,0).toString();
		} else {
			// should never happen
			return null;
		}
	}

    public static void main(String[] args) {


        ManifestEntryFrame mainMenuFrame = new ManifestEntryFrame();
    }


}